package com.ipay.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipUtil {
	
	/*文件打包zip*/
    public static String doZIP(String zipName, File [] files) throws Exception{
    //doZIP(命名的打包文件名，传递过来的File数组)
        byte[] buffer = new byte[1024];
        
        String strZipName = zipName;

        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(strZipName));

        for(int i=0;i<files.length;i++) {
	    	 if(files[i].exists()) {// 如果不存在就创建一个
	    		 FileInputStream fis = new FileInputStream(files[i]);
	
	             out.putNextEntry(new ZipEntry(System.currentTimeMillis() + files[i].getName()));
	
	             int len;
	
	             //读入需要下载的文件的内容，打包到zip文件
	             while((len = fis.read(buffer))>0) {
	                 out.write(buffer,0,len);
	             }
	             out.closeEntry();
	             fis.close();
	        }
        }
        out.close();
        return strZipName;
    }
}
